<?php require 'common/head.php'; ?>
<?php
    require '../db_connection.php';

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $id       = $_POST['id'];
        $username = $_POST['username'];
        $truename = $_POST['true_name'];
        $email    = $_POST['email'];
        $role     = $_POST['role'];
        $password  = $_POST['password'];

        $sql = "UPDATE admins SET username='$username', true_name='$truename', email='$email', role='$role'";
        if (!empty($password)) {
          $password = md5($password);
          $sql .= ", password='$password'";
        }
        $sql .= " WHERE id=$id";
        if ($conn->query($sql) === TRUE) {
            echo "<script>alert('更新成功');location.href='user_list.php';</script>";
        } else {
            echo "<script>alert('更新失败');history.back();</script>";
        }
        $conn->close();
        die();
    }

    $id   = $_GET['id'];
    $sql    = "SELECT * FROM admins WHERE id=$id";
    $result = $conn->query($sql);
    $info   = $result->fetch_assoc();

    $result->free();
    $conn->close();
?>
</head>
<body>
<div class="panel admin-panel">
  <div class="panel-head" id="add"><strong><span class="icon-pencil-square-o"></span>编辑用户-<?php echo $info['username']; ?></strong></div>
  <div class="body-content">
  <form method="post" class="form-x" action="user_edit.php" style="margin-top:20px;" enctype="multipart/form-data">  
      <div class="form-group">
        <div class="label">
          <label>用户名</label>
        </div>
        <div class="field">
          <input type="text" class="input " value="<?php echo $info['username']; ?>" name="username" data-validate="required:请输入用户名" />
          <input type="hidden" value="<?php echo $info['id']; ?>" name="id" />
          <div class="tips"></div>
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>姓名</label>
        </div>
        <div class="field">
          <input type="text" class="input " value="<?php echo $info['true_name']; ?>" name="true_name" data-validate="required:请输入名字" />
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>密码</label>
        </div>
        <div class="field">
          <input type="password" class="input " value="" name="password" />
        </div>
      </div>
      <div class="form-group" style="display:flex;align-items:center;">
        <div class="label">
          <label>角色</label>
        </div>
        <div class="field">
            <label>管理员<input type="radio" value="gly" name="role" <?php if ($info['role'] == 'gly') echo 'checked'; ?>/></label>
            <label>志愿者 <input type="radio" value="zyz" name="role" <?php if ($info['role'] == 'zyz') echo 'checked'; ?>/></label>
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label>邮箱</label>
        </div>
        <div class="field">
          <input type="text" class="input " value="<?php echo $info['email']; ?>" name="email"/>
        </div>
      </div>
      <div class="form-group">
        <div class="label">
          <label></label>
        </div>
        <div class="field">
          <button class="button bg-main" type="submit"> 提交</button>
        </div>
      </div>    
    </form>
  </div>
</div>
</body></html>   